﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Infrastructure;
using Enterprise.Model.CaiWu;
using Enterprise.Data.CaiWu;
namespace Enterprise.Service.Caiwubu
{
    public class CwZbkhService
    {
        /// <summary>
        ///  缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-CwZbkh";

        /// <summary>
        /// 列表 返回所有记录
        /// </summary>
        /// <returns></returns>
        public List<CwZbkhEntity> CwZbkhList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<CwZbkhEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                CwZbkhData data = new CwZbkhData();
                List<CwZbkhEntity> list = data.CwZbkhList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        public void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 返回记录信息
        /// </summary>
        /// <param name="zbId">指标编码</param>
        /// <returns></returns>
        public CwZbkhEntity CwZbkhDisp(int zbId,int year,int month,string dw)
        {
            return CwZbkhList().Where(p => p.ZBID == zbId&&p.KYEAR==year&&p.KMONTH==month&&p.KDW==dw).FirstOrDefault();
        }

        /// <summary>
        /// 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void CwZbkhExecute(CwZbkhEntity entity,int typeId)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into CWB_ZBKH(KHID,ZBID,KYEAR,KMONTH,KDW,KJHS,KSJS,KQZ) values(SEQ_CWB_ZBKH.nextval," + entity.ZBID + "," + entity.KYEAR + "," + entity.KMONTH + ",'" + entity.KDW + "'," + entity.KJHS + "," + entity.KSJS + "," + entity.KQZ + ")";
                    break;
                case "Update":
                    cmd = "Update CWB_ZBKH set KSJS=" + entity.KSJS + " where KHID=" + entity.KHID;
                    break;
                case "Delete":
                    cmd = "Delete from CWB_ZBKH where ZBID=" + entity.ZBID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                CwZbkhExecuteBySql(cmd, typeId);
            }
        }

        /// <summary>
        /// 组织机构操作
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1更新 0不更新</param>
        public void CwZbkhExecuteBySql(string cmd, int typeId)
        {
            CwTypeData data = new CwTypeData();
            data.CwTypeExecute(cmd);
            if (typeId == 1)
            {
                ClearCache();
            }
        }

        #region "返回指标名称"
        /// <summary>
        /// 返回名称
        /// </summary>
        /// <param name="deptId">编号</param>
        /// <returns></returns>
        public static decimal GetZbkhValue(int zbId,int year,int month,string dw)
        {
            CwZbkhService dService = new CwZbkhService();
            var q = dService.CwZbkhDisp(zbId, year, month, dw);
            if (q != null)
            {
                return q.KSJS;
            }
            return 0;
        }

        /// <summary>
        /// 返回名称
        /// </summary>
        /// <param name="deptId">编号</param>
        /// <returns></returns>
        public static decimal GetZbkhSumValue(int zbId, int year, string dw)
        {
            CwZbkhService dService = new CwZbkhService();
            var q = dService.CwZbkhList().Where(p => p.ZBID == zbId && p.KYEAR == year && p.KDW == dw&&p.KMONTH>0);
            return q.Sum(p => p.KSJS);
        }
        #endregion
    }
}
